#include "U2StringConverter.h"




U2EG_NAMESPACE_USING



//-----------------------------------------------------------------------
U2String U2StringConverter::toString(u2real val, unsigned short precision, 
                                 unsigned short width, char fill, std::ios::fmtflags flags)
{
#if U2_PLATFORM == U2_PLATFORM_ANDROID
    static char buffer[128] = "";
    int n = snprintf(buffer, 128, "%f", val);
    return U2String(buffer, n);
#else
    U2StringStream stream;
    stream.precision(precision);
    stream.width(width);
    stream.fill(fill);
    if (flags)
        stream.setf(flags);
    stream << val;
    return stream.str();
#endif
}
//-----------------------------------------------------------------------
U2String U2StringConverter::toString(int val, 
                                 unsigned short width, char fill, std::ios::fmtflags flags)
{
    U2StringStream stream;
    stream.width(width);
    stream.fill(fill);
    if (flags)
        stream.setf(flags);
    stream << val;
    return stream.str();
}
//-----------------------------------------------------------------------
#if U2_ARCH_TYPE == U2_ARCHITECTURE_64 || U2_PLATFORM == U2_PLATFORM_APPLE || U2_PLATFORM == U2_PLATFORM_APPLE_IOS
U2String U2StringConverter::toString(unsigned int val, 
                                 unsigned short width, char fill, std::ios::fmtflags flags)
{
    U2StringStream stream;
    stream.width(width);
    stream.fill(fill);
    if (flags)
        stream.setf(flags);
    stream << val;
    return stream.str();
}
//-----------------------------------------------------------------------
U2String U2StringConverter::toString(size_t val, 
                                 unsigned short width, char fill, std::ios::fmtflags flags)
{
    U2StringStream stream;
    stream.width(width);
    stream.fill(fill);
    if (flags)
        stream.setf(flags);
    stream << val;
    return stream.str();
}
#   if U2_COMPILER == U2_COMPILER_MSVC
//-----------------------------------------------------------------------
U2String U2StringConverter::toString(unsigned long val, 
                                 unsigned short width, char fill, std::ios::fmtflags flags)
{
    U2StringStream stream;
    stream.width(width);
    stream.fill(fill);
    if (flags)
        stream.setf(flags);
    stream << val;
    return stream.str();
}

#   endif
//-----------------------------------------------------------------------
#else
U2String U2StringConverter::toString(size_t val, 
                                 unsigned short width, char fill, std::ios::fmtflags flags)
{
    U2StringStream stream;
    stream.width(width);
    stream.fill(fill);
    if (flags)
        stream.setf(flags);
    stream << val;
    return stream.str();
}
//-----------------------------------------------------------------------
U2String U2StringConverter::toString(unsigned long val, 
                                 unsigned short width, char fill, std::ios::fmtflags flags)
{
    U2StringStream stream;
    stream.width(width);
    stream.fill(fill);
    if (flags)
        stream.setf(flags);
    stream << val;
    return stream.str();
}
#endif
//-----------------------------------------------------------------------
U2String U2StringConverter::toString(long val, 
                                 unsigned short width, char fill, std::ios::fmtflags flags)
{
    U2StringStream stream;
    stream.width(width);
    stream.fill(fill);
    if (flags)
        stream.setf(flags);
    stream << val;
    return stream.str();
}
//-----------------------------------------------------------------------
U2String U2StringConverter::toString(bool val, bool yesNo)
{
    if (val)
    {
        if (yesNo)
        {
            return "yes";
        }
        else
        {
            return "true";
        }
    }
    else
        if (yesNo)
        {
            return "no";
        }
        else
        {
            return "false";
        }
}
//-----------------------------------------------------------------------
U2String U2StringConverter::toString(const U2StringVector& val)
{
    U2StringStream stream;
    U2StringVector::const_iterator i, iend, ibegin;
    ibegin = val.begin();
    iend = val.end();
    for (i = ibegin; i != iend; ++i)
    {
        if (i != ibegin)
            stream << " ";

        stream << *i; 
    }
    return stream.str();
}
//-----------------------------------------------------------------------
u2real U2StringConverter::parseReal(const U2String& val, u2real defaultValue)
{
#if U2_PLATFORM == U2_PLATFORM_ANDROID
    u2real ret = 0;
    int n = sscanf(val.c_str(), "%f", &ret);

    if(n == 0){
        // Nothing read, so try integer format
        int ret2 = 0;
        n = sscanf(val.c_str(), "%d", &ret2);
        if(n == 1)
            ret = (u2real)ret2;
    }
#else
    // Use istringstream for direct correspondence with toString
    U2StringStream str(val);
    u2real ret = defaultValue;
    str >> ret;
#endif
    return ret;
}
//-----------------------------------------------------------------------
int U2StringConverter::parseInt(const U2String& val, int defaultValue)
{
    // Use istringstream for direct correspondence with toString
    U2StringStream str(val);
    int ret = defaultValue;
    str >> ret;

    return ret;
}
//-----------------------------------------------------------------------
unsigned int U2StringConverter::parseUnsignedInt(const U2String& val, unsigned int defaultValue)
{
    // Use istringstream for direct correspondence with toString
    U2StringStream str(val);
    unsigned int ret = defaultValue;
    str >> ret;

    return ret;
}
//-----------------------------------------------------------------------
long U2StringConverter::parseLong(const U2String& val, long defaultValue)
{
    // Use istringstream for direct correspondence with toString
    U2StringStream str(val);
    long ret = defaultValue;
    str >> ret;

    return ret;
}
//-----------------------------------------------------------------------
unsigned long U2StringConverter::parseUnsignedLong(const U2String& val, unsigned long defaultValue)
{
    // Use istringstream for direct correspondence with toString
    U2StringStream str(val);
    unsigned long ret = defaultValue;
    str >> ret;

    return ret;
}
//-----------------------------------------------------------------------
bool U2StringConverter::parseBool(const U2String& val, bool defaultValue)
{
    if ((U2StringUtil::startsWith(val, "true") || U2StringUtil::startsWith(val, "yes")
        || U2StringUtil::startsWith(val, "1")))
        return true;
    else if ((U2StringUtil::startsWith(val, "false") || U2StringUtil::startsWith(val, "no")
        || U2StringUtil::startsWith(val, "0")))
        return false;
    else
        return defaultValue;
}
//-----------------------------------------------------------------------
U2StringVector U2StringConverter::parseStringVector(const U2String& val)
{
    return U2StringUtil::split(val);
}
//-----------------------------------------------------------------------
bool U2StringConverter::isNumber(const U2String& val)
{
#if U2_PLATFORM == U2_PLATFORM_ANDROID
    float test;
    int n = sscanf(val.c_str(), "%f", &test);
    return n == 1;
#else
    U2StringStream str(val);
    float tst;
    str >> tst;
    return !str.fail() && str.eof();
#endif
}
